home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / memdv371.lha / memory-device / source / device.c < prev    next >
C/C++ Source or Header  |  1995-12-20  |  6KB  |  271 lines

  1. /*
  2. ** $VER: device.c 1.0 (20 Dec 1995)
  3. **
  4. ** memory.device - direct memory access
  5. **
  6. ** (C) Copyright 1995 Marius Gröger
  7. **     All Rights Reserved
  8. **
  9. ** Inspired by z2ram device in Linux/68k/Amiga
  10. **
  11. ** 20 Dec 1995 : 001.000 :  created
  12. */
  13.  
  14. #define DEBUG 0
  15.  
  16. /*F*/ /* includes */
  17. #ifndef CLIB_ALIB_PROTOS_H
  18. #include <clib/alib_protos.h>
  19. #endif
  20. #ifndef CLIB_EXEC_PROTOS_H
  21. #include <clib/exec_protos.h>
  22. #include <pragmas/exec_sysbase_pragmas.h>
  23. #endif
  24.  
  25. #ifndef EXEC_MEMORY_H
  26. #include <exec/memory.h>
  27. #endif
  28. #ifndef EXEC_IO_H
  29. #include <exec/io.h>
  30. #endif
  31. #ifndef EXEC_ERRORS_H
  32. #include <exec/errors.h>
  33. #endif
  34. #ifndef DEVICES_TRACKDISK_H
  35. #include <devices/trackdisk.h>
  36. #endif
  37.  
  38. #ifndef _STRING_H
  39. #include <string.h>
  40. #endif
  41.  
  42. #ifndef __MEMORY_H
  43. #include "memory.h"
  44. #endif
  45. #ifndef __DEBUG_H
  46. #include "debug.h"
  47. #endif
  48. #ifndef __COMPILER_H
  49. #include "compiler.h"
  50. #endif
  51. /*E*/
  52.  
  53. /*F*/ /* imports */
  54. /*E*/
  55. /*F*/ /* exports */
  56. PUBLIC ASM SAVEDS struct Device *DevInit(REG(d0) BASEPTR, REG(a0) ULONG seglist, REG(a6) struct Library *_SysBase);
  57. PUBLIC ASM SAVEDS LONG DevOpen(REG(a1) struct IOExtTD *iotd, REG(d0) ULONG unit, REG(d1) ULONG flags, REG(a6) BASEPTR);
  58. PUBLIC ASM SAVEDS BPTR DevExpunge(REG(a6) BASEPTR);
  59. PUBLIC ASM SAVEDS BPTR DevClose( REG(a1) struct IOExtTD *ior, REG(a6) BASEPTR);
  60. PUBLIC VOID DevTermIO(BASEPTR, struct IOExtTD *iotd);
  61. PUBLIC ASM SAVEDS VOID DevBeginIO(REG(a1) struct IOExtTD *iotd, REG(a6) BASEPTR);
  62. PUBLIC ASM SAVEDS LONG DevAbortIO(REG(a1) struct IOExtTD *ior, REG(a6) BASEPTR);
  63. /*E*/
  64. /*F*/ /* private */
  65. /*E*/
  66.  
  67.  
  68.    /*
  69.    ** initialise device
  70.    */
  71. /*F*/ PUBLIC ASM SAVEDS struct Device *DevInit(REG(d0) BASEPTR, REG(a0) ULONG seglist, REG(a6) struct Library *_SysBase)
  72. {
  73.    BOOL ok;
  74.    UBYTE *p;
  75.    UWORD i;
  76.  
  77.    d(("entered device, initialising device base...\n"));
  78.  
  79.       /* clear data base */
  80.    for(p = ((UBYTE*)mb) + sizeof(struct Library), i = sizeof(struct MemoryBase)-sizeof(struct Library); i; i--)
  81.       *p++ = 0;
  82.  
  83.    SysBase = _SysBase;
  84.  
  85.    mb->mb_SegList = seglist;           /* store DOS segment list */
  86.  
  87.    InitSemaphore(&mb->mb_Lock);
  88.  
  89.    ok = TRUE;
  90.  
  91.    d(("left %ld\n",ok));
  92.  
  93.    return (struct Device *)(ok ? mb : NULL);
  94. }
  95. /*E*/
  96.  
  97.    /*
  98.    ** open device
  99.    */
  100. /*F*/ PUBLIC ASM SAVEDS LONG DevOpen(REG(a1) struct IOExtTD *iotd, REG(d0) ULONG unit, REG(d1) ULONG flags, REG(a6) BASEPTR)
  101. {
  102.    LONG rv = 0;
  103.  
  104.    d(("entered\n"));
  105.  
  106.    /* Make sure our open remains single-threaded. */
  107.    ObtainSemaphore(&mb->mb_Lock);
  108.  
  109.    mb->mb_DevNode.lib_OpenCnt++;
  110.    mb->mb_DevNode.lib_Flags &= ~LIBF_DELEXP;
  111.    iotd->iotd_Req.io_Error = 0;
  112.    iotd->iotd_Req.io_Unit = NULL;
  113.    iotd->iotd_Req.io_Device = (struct Device *)mb;
  114.    iotd->iotd_Req.io_Message.mn_Node.ln_Type = NT_REPLYMSG;
  115.  
  116.    ReleaseSemaphore(&mb->mb_Lock);
  117.  
  118.    return rv;
  119. }
  120. /*E*/
  121.  
  122.    /*
  123.    ** close device
  124.    */
  125. /*F*/ PUBLIC ASM SAVEDS BPTR DevClose(REG(a1) struct IOExtTD *ior, REG(a6) BASEPTR)
  126. {
  127.    BPTR seglist;
  128.  
  129.    d2(("entered\n"));
  130.  
  131.    ObtainSemaphore(&mb->mb_Lock);
  132.  
  133.       /* invalidate IO request block */
  134.    ior->iotd_Req.io_Device = (struct Device *)-1;
  135.    ior->iotd_Req.io_Unit = (struct Unit *)-1;
  136.  
  137.    mb->mb_DevNode.lib_OpenCnt--;
  138.  
  139.    ReleaseSemaphore(&mb->mb_Lock);
  140.  
  141.    if (mb->mb_DevNode.lib_Flags & LIBF_DELEXP)
  142.       seglist = DevExpunge(mb);
  143.    else
  144.       seglist = 0;
  145.  
  146.    return seglist;
  147. }
  148. /*E*/
  149. /*F*/ PUBLIC ASM SAVEDS BPTR DevExpunge(REG(a6) BASEPTR)
  150. {
  151.    BPTR seglist;
  152.  
  153.    d2(("entered\n"));
  154.  
  155.    if (mb->mb_DevNode.lib_OpenCnt)
  156.    {
  157.       mb->mb_DevNode.lib_Flags |= LIBF_DELEXP;
  158.       seglist = 0;
  159.    }
  160.    else
  161.    {
  162.          /* detach device from system list */
  163.       Remove((struct Node*)mb);
  164.  
  165.          /* save seglist for return value */
  166.       seglist = (long)mb->mb_SegList;
  167.  
  168.          /* return memory
  169.          **
  170.          ** NO FURTHER ACCESS TO DEVICE BASE ALLOWED!
  171.          */
  172.       FreeMem(((char *)mb) - mb->mb_DevNode.lib_NegSize,
  173.          (ULONG)(mb->mb_DevNode.lib_PosSize + mb->mb_DevNode.lib_NegSize));
  174.    }
  175.  
  176.    return seglist;
  177. }
  178. /*E*/
  179.  
  180.    /*
  181.    ** initiate io command (1st level dispatcher)
  182.    */
  183. #if 0
  184. /*F*/ static INLINE VOID DevForwardIO(BASEPTR, struct IOExtTD *iotd)
  185. {
  186.    d(("forwarding request %ld\n", iotd->iotd_Req.io_Command));
  187.  
  188.    /* request is no longer of type "quick i/o" */
  189.    iotd->iotd_Req.io_Flags &= ~IOF_QUICK;
  190.    PutMsg(mb->mb_ServerPort, (struct Message*)iotd);
  191. }
  192. /*E*/
  193. #endif
  194. /*F*/ PUBLIC VOID DevTermIO(BASEPTR, struct IOExtTD *iotd)
  195. {
  196.    d(("cmd = %ld, error = %ld, wireerror = %ld\n", iotd->iotd_Req.io_Command, iotd->iotd_Req.io_Error,iotd->iotd_WireError));
  197.  
  198.                   /* if this command was done asynchonously, we must
  199.                   ** reply the request
  200.                   */
  201.    if(!(iotd->iotd_Req.io_Flags & IOF_QUICK))
  202.       ReplyMsg((struct Message *)iotd);
  203.    else           /* otherwise just mark it as done */
  204.       iotd->iotd_Req.io_Message.mn_Node.ln_Type = NT_REPLYMSG;
  205. }
  206. /*E*/
  207. /*F*/ PUBLIC ASM SAVEDS VOID DevBeginIO(REG(a1) struct IOExtTD *iotd, REG(a6) BASEPTR)
  208. {
  209.       /* mark request as active */
  210.    iotd->iotd_Req.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  211.    iotd->iotd_Req.io_Error = 0;
  212.  
  213.    d(("cmd = %ld\n",iotd->iotd_Req.io_Command));
  214.  
  215.    switch(iotd->iotd_Req.io_Command)
  216.    {
  217.       case CMD_READ:
  218.          CopyMemQuick((UBYTE*)iotd->iotd_Req.io_Offset, (UBYTE*)iotd->iotd_Req.io_Data,
  219.                   (ULONG)iotd->iotd_Req.io_Length);
  220.          iotd->iotd_Req.io_Actual = iotd->iotd_Req.io_Length;
  221.       break;
  222.  
  223.       case CMD_WRITE:
  224.          CopyMemQuick((UBYTE*)iotd->iotd_Req.io_Data, (UBYTE*)iotd->iotd_Req.io_Offset,
  225.                   (ULONG)iotd->iotd_Req.io_Length);
  226.          iotd->iotd_Req.io_Actual = iotd->iotd_Req.io_Length;
  227.       break;
  228.  
  229.       case CMD_RESET:
  230.       case CMD_UPDATE:
  231.       case CMD_CLEAR:
  232.       case CMD_STOP:
  233.       case CMD_START:
  234.       case CMD_FLUSH:
  235.       break;
  236.  
  237.       case TD_PROTSTATUS:
  238.          iotd->iotd_Req.io_Actual = 0; /* not protected */
  239.       break;
  240.  
  241.       case TD_MOTOR:
  242.          iotd->iotd_Req.io_Actual = 0;
  243.       break;
  244.  
  245.       case TD_FORMAT:
  246.          iotd->iotd_Req.io_Actual = iotd->iotd_Req.io_Length;
  247.       break;
  248.  
  249.       default:
  250.          iotd->iotd_Req.io_Error = IOERR_NOCMD;
  251.       break;
  252.    }
  253.  
  254.    if (iotd) DevTermIO(mb, iotd);
  255.  
  256.    return;
  257. }
  258. /*E*/
  259.  
  260.    /*
  261.    ** stop io-command
  262.    */
  263. /*F*/ PUBLIC ASM SAVEDS LONG DevAbortIO(REG(a1) struct IOExtTD *ior, REG(a6) BASEPTR)
  264. {
  265.    d(("cmd = %ld\n",ior->iotd_Req.io_Command));
  266.  
  267.    return 0;
  268. }
  269. /*E*/
  270.  
  271.